/** @author castellan@usi.ch
 * @author daniele.gili@usi.ch
 * @author blancsv@usi.ch
 */

package ch.usi.inf.pf2;

import java.awt.Graphics;
import java.util.Timer;

public class Nand extends CommonGate implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Nand(Timer a, int x, int y, int pos) {
		this.myTimer = a;
		myTimer.schedule(new ActionDelay(this), 200L, 200L);
		this.x = x - getWidth() / 2;
		this.y = y - getHeight() / 2;
		this.pos = pos;
	}

	public Nand(Timer a) {
		this.myTimer = a;
		myTimer.schedule(new ActionDelay(this), 200L, 200L);
		this.x = 0;
		this.y = 0;
		this.pos = 0;
	}

	public void setNewValue() {

		if (inputList.size() == 0) {
			actualValue = null;
			return;
		}

		for (int a = 0; a <= inputList.size() - 1; a++) {
			if (inputList.get(a).getValue(actualIndex) == null) {
				actualValue = null;
				return;
			}
		}

		if (inputList.get(0).getValue(actualIndex) == 1
				&& inputList.get(1).getValue(actualIndex) == 1) {
			actualValue = 0;
		}

		else {
			actualValue = 1;
			return;
		}
	}

	public Integer compute(Integer input1, Integer input2) {

		this.outputValue = new Integer(1);
		if (input1 == 1 && input2 == 1)
			this.outputValue = 0;

		return this.outputValue;
	}

	// ////////////////////////////////////////

	public void draw(final Graphics g) {
		int y1 = y + 40;
		int x2 = x + 30;
		int y2 = y + 15;
		g.drawLine(x, y, x, y1);
		g.drawArc(x - 30, y, 60, 40, -90, 180);
		g.drawOval(x2, y2, 10, 10);
	}

	public boolean contains(final int x, final int y) {
		return x >= this.x && x <= this.x + width && y >= this.y
				&& y <= this.y + height;
	}

}
